package com._21cn.framework.base.service.impl;

import com._21cn.framework.base.db.BaseDao;
import com._21cn.framework.base.service.BaseService;
import com._21cn.framework.flip.PageList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Map;

/**
 * BaseServiceImpl 提供BaseService的基本实现
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {
    /**
     * 日志
     */
    protected Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 如果需要直接使用BaseService< T >定义的业务方法，必须在子类中覆盖本方法。为了兼容使用了旧版本的应用程序，这里并没有把本方法定义为abstract。
     * @return 返回在子类中实际的注入的BaseDao< T >
     */
    protected BaseDao<T> getBaseDao(){
    	throw new UnsupportedOperationException( "If need use directly BaseService's method, the sub-class must override this method." );
    }

	public void insert( T object ){
		getBaseDao().insert( object );
	}
	
    public int batchInsert( T... objects ){
		return getBaseDao().batchInsert( objects );
	}
	
    public int batchInsert( Collection<T> objects ){
    	return getBaseDao().batchInsert( objects );
	}
	
	public int update( T object ){
		return getBaseDao().update( object );
	}

    public int batchUpdate( T... objects ){
    	return getBaseDao().batchUpdate( objects );
	}
	
    public int batchUpdate( Collection<T> objects ){
    	return getBaseDao().batchUpdate( objects );
	}
	
	public int update( Map<String,Object> param){
		return getBaseDao().updateByMap( param );
	}
	
	public int delete( Map<String,Object> param ){
		return getBaseDao().deleteByMap( param );
	}

    public int batchDelete( Object[] ids ){
    	return getBaseDao().batchDeleteById( ids );
	}
	
    public int batchDelete( Collection<?> ids ){
    	return getBaseDao().batchDeleteById( ids );
	}
	
	public int delete( Object id ){
		return getBaseDao().deleteById( id );
	}
	
	public T getById( Object id ){
		return getBaseDao().selectById( id );
	}
	
	public int getCount( Map<String,Object> param ){
		return getBaseDao().selectCountByMap( param );
	}

	public PageList<T> getList(Map<String,Object> params, int pageNo, int pageSize, boolean doCount){
		return getBaseDao().selectByMap( params, pageNo, pageSize, doCount );
	}

	public PageList<T> getList(Map<String,Object> params, int pageNo, int pageSize) {
		return getBaseDao().selectByMap( params, pageNo, pageSize );
	}
    
}
